module.exports = (EdtApi) => {
    "use strict";
    let chatId = "", count = 0;
    function isGroup() {
        return false;
    }
    async function getChatId(times = 0) {
        let url = window.location.href;
        const regex = /\/messages\/([^\/]+)\/?/;
        const match = regex.exec(url);
        let id = '';
        try {
            if (match) id = match[1];
        } catch (error) {
            console.log('获取对话id失败', error)
        }
        if (id && id != chatId) return id;
        await EdtApi.sleep(50);
        if (times > 10) return id;
        times++;
        return getChatId(times);
    }
    return {
        config: {
            friendList: 'div.css-175oi2r.r-sa2ff0 a[role="link"]',
            chat: 'main[role="main"] div.css-175oi2r[data-testid="convoScreen"] div.css-175oi2r[style="flex: 1 1 0%;"]',
            input: 'div.css-175oi2r[data-testid="convoScreen"]>div.css-175oi2r>div.css-175oi2r textarea[dirname="ltr"]',
            msgBox: 'div.css-175oi2r[data-testid="convoScreen"]>div.css-175oi2r>div.css-175oi2r>div:nth-child(2)>div:nth-child(2)',
            inMsg: 'div.css-175oi2r[data-testid="convoScreen"]>div.css-175oi2r>div.css-175oi2r>div:nth-child(2)>div:nth-child(2) div[style*="flex-start"] div.css-146c3p1:not(.trans-inited)',
            outMsg: 'div.css-175oi2r[data-testid="convoScreen"]>div.css-175oi2r>div.css-175oi2r>div:nth-child(2)>div:nth-child(2) div[style*="flex-end"] div.css-146c3p1:not(.trans-inited)',
            sendBtn: 'div.css-175oi2r[data-testid="convoScreen"]>div.css-175oi2r>div.css-175oi2r>div:nth-child(3) button:last-child',
            setting: 'div.css-175oi2r[data-testid="convoScreen"]>div.css-175oi2r>div.css-175oi2r textarea[dirname="ltr"]',
            tips: {
                in: 'a.css-175oi2r.r-1loqt21.r-1otgn73[role="link"] div.css-175oi2r[style="flex: 1 1 0%;"]',
                out: 'div.css-175oi2r[data-testid="convoScreen"]>div.css-175oi2r>div.css-175oi2r>div:nth-child(3)'
            },
            crm: {
                // listener: '#LeftColumn-main',
                top: '#detail-header>div.css-175oi2r',
                left: ""
            },
            placeholder: {
                attr: 'placeholder',
                selector: 'div.css-175oi2r[data-testid="convoScreen"]>div.css-175oi2r>div.css-175oi2r textarea[dirname="ltr"]',
            },
            autoCount: 20,
            extra: {//其他会触发对话框变动的情况处理
                selector: 'div.css-175oi2r.r-sa2ff0 a[role="link"]',
                level: 20,
                delay: 600,
            },
        },
        getUnread() {
            count = 0;
            const noticeEl = document.querySelector('nav[role="navigation"]>a[href="/messages"] div.css-146c3p1.r-dnmrzs.r-1udh08x.r-1udbk01.r-3s2u2q.r-1iln25a[dir="auto"]');
            if (noticeEl) count += EdtApi.safeParseInt(noticeEl.textContent);
            return count;
        },
        getChatInfo() {
            return new Promise((resolve) => {
                EdtApi.queryWait({
                    selector: this.config.input,
                    timeout: 120000
                }).then(async (input) => {
                    if (!input) return resolve({ id: "", isGroup: false });
                    chatId = await getChatId();
                    resolve({ id: chatId, isGroup: isGroup() });
                }).catch(() => {
                    resolve({ id: "", isGroup: false });
                });
            });
        },
        getChatTitle(chatEl = document) {
            let titleEl = chatEl.querySelector('a.css-175oi2r.r-1loqt21.r-1otgn73[role="link"] div.css-175oi2r[style="flex: 1 1 0%;"]>div.r-1iln25a:first-child');
            return titleEl?.textContent || "";
        },
        getText(msgBox) {
            let text = '';
            try {
                if (msgBox.querySelector('img')) {
                    let childNodes = msgBox.childNodes;
                    childNodes?.forEach(child => {
                        if (child.nodeName == "IMG") {
                            text += child.getAttribute("alt") || "";
                        } else {
                            text += child.textContent;
                        }
                    });
                } else {
                    text = msgBox.textContent;
                }
            } catch (error) {
                console.error(error);
            }
            return text;
        },
        getIptMsg(inputEl) {
            return inputEl?.value || "";
        },
        getUserInfo() {
            try {
                let userEl = document.querySelector('nav[role="navigation"]>a:first-child');
                let userName = "", avatar = "", account = "";
                if (userEl) {
                    let url = userEl.getAttribute("href");
                    userName = url?.replace("/profile/", "");
                    account = userName;
                    let avatarEl = userEl.querySelector('img.css-9pa8cd');
                    avatar = avatarEl?.getAttribute("src") || "";
                }
                return {
                    avatar,
                    userName,
                    account
                };
            } catch (error) {
                return null;
            }
        }
    }
};